// https://leetcode.cn/problems/combinations/description/

// 算法思路总结：
// 1. 回溯算法生成从1到n中所有k个数的组合
// 2. 从pos开始遍历，避免重复和保证顺序
// 3. 路径长度等于k时保存当前组合
// 4. 通过起始位置控制，确保组合元素递增排列
// 5. 时间复杂度：O(C(n,k)×k)，空间复杂度：O(k)

#include <iostream>
using namespace std;

#include <cstring>
#include <vector>
#include <algorithm>

class Solution 
{
public:
    vector<vector<int>> ret;
    vector<int> path;
    int _n, _k;
    vector<vector<int>> combine(int n, int k) 
    {
        ret.clear();
        _n = n, _k = k;

        dfs(1);

        return ret;
    }

    void dfs(int pos)
    {
        if (path.size() == _k)
        {
            ret.push_back(path);
            return ;
        }

        for (int i = pos ; i <= _n ; i++)
        {
            path.push_back(i);
            dfs(i + 1);
            path.pop_back();
        }
    }
};

void printResult(const vector<vector<int>>& result) 
{
    cout << "[";
    for (int i = 0; i < result.size(); ++i) 
    {
        cout << "[";
        for (int j = 0; j < result[i].size(); ++j) 
        {
            cout << result[i][j];
            if (j < result[i].size() - 1) cout << ",";
        }
        cout << "]";
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

int main()
{
    int n1 = 4, n2 = 1;
    int k1 = 2, k2 = 1;

    Solution sol;

    auto vv1 = sol.combine(n1, k1);
    auto vv2 = sol.combine(n2, k2);

    printResult(vv1);
    printResult(vv2);

    return 0;
}